clear all
use "/Users/yasenov/Dropbox/data_dta/master.dta", clear
cd "/Users/yasenov/Desktop"

set matsize 4000
set more off

tostring id grade year, replace
gen course = grade + " " + subject + " " + cohort
gen class = year + " " + course
gen studentxclass = id + " " + class
gen gradebook = year + grade + cohort
destring id grade year, replace

*Only want to keep studentXclass observations where we have variation within studentXclass?
*drop if stdxclass_sd==0

label var late_start "\ Late Block"
label var male "Male"
label var bulg "Native"
label var month "Month"
label var month2 "Month$^2$"
label var stem "STEM"
label var semester "Semester 2"
label var num_month "Number Marks"
label var num_month2 "Number Marks$^2$"
label var vv_early "Early"
label var v_early "Early"
label var early "Early"
label var order_ass "Assignment Order"

global controls num_month order_ass
* order_ass  month month2 num_month2

/* Identify classses that meet only for 1 semester 
sort class month
by class: gen last_month = month[_N]
by class: gen first_month = month[1]

/* this drops the last month of one semester in the first semester classes */
drop if month == 5 & (last_month == 4 | last_month == 5)
/* this drops the first month of one semester classes in the 2nd semester*/
drop if month == 6 & first_month == 6
*/

*need age bins
gen age14 = 0
replace age14 = 1 if age<15
gen age15 = 0 
replace age15 = 1 if age>=15 & age<16
gen age16 = 0 
replace age16 = 1 if age>=16 & age<17
gen age17 = 0
replace age17 = 1 if age>=17 & age<18
gen age18 = 0
replace age18 = 1 if age>=18

gen below_med_age = 0
replace below_med_age=1 if age<14

*Want to generate variable that tells me how good the student is
bysort id: egen student_ability = mean(marks)
preserve
collapse (mean) marks, by(id)
sum marks, d
restore
gen low_ability = 0 
replace low_ability = 1 if student_ability<4.5 & student_ability!=.

******************************************************
************** Baseline Analysis  ********************
******************************************************
eststo clear

eststo: xi: cgmreg Zmarks i.late_start i.id, absorb(class) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start i.id $controls, absorb(class) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start, absorb(studentxclass) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start $controls, absorb(studentxclass) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start i.id $controls i.month, absorb(class) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start $controls i.month, absorb(studentxclass) cluster(id class)

la var _Ilate_star_1 "\ Late Block"

esttab using "Results.tex", ///
	keep(_Ilate_star_1) replace b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	refcat(_Ilate_star_1 "Model 1", nolabel) noconstant nogaps noobs ///
	mgroups("Standardized Grade", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
    mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") label postfoot("")

*******************************************************
*************** AGE INTERACTIONS **********************
*******************************************************
eststo clear

eststo: xi: cgmreg Zmarks i.late_start*hs i.id, absorb(class) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start*hs i.id $controls, absorb(class) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start*hs, absorb(studentxclass) cluster(id class)
eststo: xi: cgmreg Zmarks i.late_start*hs $controls, absorb(studentxclass) cluster(id class)
*eststo: xi: cgmreg Zmarks i.late_start*hs i.id $controls i.month, absorb(class) cluster(id class)
*eststo: xi: cgmreg Zmarks i.late_start*hs $controls i.month, absorb(studentxclass) cluster(id class)

la var _Ilate_star_1 "\ Late Block"
la var _IlatXhs_1 "\ \ \ X High Schoolers"

esttab using "Results.tex", ///
	keep(_Ilate_star_1 _IlatXhs_1) append b(3) se(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	refcat(_Ilate_star_1 "\hline Model 2 - Age Interactions", nolabel) noobs noconstant nolines nogaps ///
	mtitles("" "" "" "" "" "") label prehead("") postfoot("") 

*******************************************************
*************** PERIOD INTERACTIONS *******************
*******************************************************
eststo clear

eststo: xi: cgmreg Zmarks i.late_start*i.v_early i.id, absorb(class) cluster(id class)
estadd local controls " "
estadd local studentfe "X"
estadd local classfe "X"
estadd local studentXclassfe " "
estadd local monthfe " "
eststo: xi: cgmreg Zmarks i.late_start*i.v_early i.id $controls, absorb(class) cluster(id class)
estadd local controls "X"
estadd local studentfe "X"
estadd local classfe "X"
estadd local studentXclassfe " "
estadd local monthfe " "
eststo: xi: cgmreg Zmarks i.late_start*i.v_early, absorb(studentxclass) cluster(id class)
estadd local controls " "
estadd local studentfe " "
estadd local classfe " "
estadd local studentXclassfe "X"
estadd local monthfe " "
eststo: xi: cgmreg Zmarks i.late_start*i.v_early $controls, absorb(studentxclass) cluster(id class)
estadd local controls "X"
estadd local studentfe " "
estadd local classfe " "
estadd local studentXclassfe "X"
estadd local monthfe " "
eststo: xi: cgmreg Zmarks i.late_start*i.v_early i.id $controls i.month, absorb(class) cluster(id class)
estadd local controls "X"
estadd local studentfe "X"
estadd local classfe "X"
estadd local studentXclassfe " "
estadd local monthfe "X"
eststo: xi: cgmreg Zmarks i.late_start*i.v_early $controls i.month, absorb(studentxclass) cluster(id class)
estadd local controls "X"
estadd local studentfe " "
estadd local classfe " "
estadd local studentXclassfe "X"
estadd local monthfe "X"

la var _Ilate_star_1 "\ Late Block"
la var _IlatXv_e_1_1 "\ \ \ X Period 1-2"

esttab using "Results.tex", ///
	keep(_Ilate_star_1 _IlatXv_e_1_1) append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes ///
	refcat(_Ilate_star_1 "\hline Model 3 - Period Interactions", nolabel) noconstant nogaps ///
	scalars("controls Controls" "classfe Class FE" "studentfe Student FE" "studentXclassfe Student X Class FE" "monthfe Month FE") ///
   mtitles("" "" "" "" "" "") label prehead("") posthead("") 
